#!/usr/bin/python
import datetime
import sys

def generate_data(subscriber_id,msisdn,dual_number,now):
    #all fields of dual_suberscriber   
    msisdn='34'+msisdn
    dual_number='34'+dual_number   
    today_str=now.split(" ")[0]

    dual_sub_str="%d|%s|%s|1|0||00000000000000000000000000000000|1|000000000000|0|0"%(subscriber_id, msisdn,dual_number)        
    #SUBSCRIBERID, RESOURCENUMBER,CREATEDATE,RESERVED1 in bme_suberscriber       
    bme_sub_str="%d|%s|%s|migration"%(subscriber_id,msisdn,now)
    #SERIALID,SUBSCRIBERID,CREATEDATE,CREATEOPERID,SUBSCRIPTIONID1, SERVICEID1,EFFECTIVEDATE1,EXPIREDATE1 in bme_suberscriberrela       
    bme_sub_rela_str="%d|%d|%s|migration|Dual|41119|%s|2110-01-01"%(subscriber_id,subscriber_id,today_str,today_str) 
    return dual_sub_str, bme_sub_str, bme_sub_rela_str
    
    
def convert(inputfile):
    file_org=open(inputfile,'r')
    file_dual_suberscriber=open('dual_suberscriber.unl','w')
    file_bme_suberscriber=open('bme_suberscriber.unl','w')
    file_bme_suberscriberrela=open('bme_suberscriberrela.unl','w')
    subscriber_id=10000000000
    now = datetime.datetime.now()
    str_now=now.strftime("%Y-%m-%d %H:%M:%S")
    line=file_org.readline()
    while(line!=None and line!=''):
        msdn_business,msdn_personal,name=line.split('#')          
        dual_sub_str, bme_sub_str, bme_sub_rela_str = generate_data(subscriber_id, msdn_business,msdn_personal, str_now )         
        file_dual_suberscriber.write(dual_sub_str+'\n')        
        file_bme_suberscriber.write(bme_sub_str+'\n') 
        file_bme_suberscriberrela.write(bme_sub_rela_str+'\n')       
       
        subscriber_id=subscriber_id+1
        
        dual_sub_str, bme_sub_str, bme_sub_rela_str = generate_data(subscriber_id,msdn_personal, msdn_business,str_now )         
        file_dual_suberscriber.write(dual_sub_str+'\n')        
        file_bme_suberscriber.write(bme_sub_str+'\n') 
        file_bme_suberscriberrela.write(bme_sub_rela_str+'\n') 
        subscriber_id=subscriber_id+1
        line=file_org.readline()

    file_org.close()
    file_dual_suberscriber.close()
    file_bme_suberscriber.close()
    file_bme_suberscriberrela.close()
    
    

if __name__ == "__main__":
   argv = sys.argv
   if len(argv)==2:   
       input_file=argv[1]
       convert(input_file)  
      
   else:
      print "usage: %s input_file"% argv[0]   
